package com.situ.mall.controller;


import com.situ.mall.constant.RedisConstants;
import com.situ.mall.pojo.Admin;
import com.situ.mall.pojo.Permission;
import com.situ.mall.pojo.query.AdminQuery;
import com.situ.mall.service.IAdminService;
import com.situ.mall.service.IPermissionService;
import com.situ.mall.util.PageResult;
import com.situ.mall.util.Result;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Gao
 * @since 2025-06-17
 */
@RestController
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    private IAdminService adminService;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private IPermissionService permissionService;


    @RequestMapping("/logout")
    public Result logout(HttpSession session) {
        session.removeAttribute("admin");
        return Result.ok("退出成功");
    }

    @RequestMapping("/login")
    public Result login(String name, String password, String code, Integer type, HttpSession session) {
        //先判断验证码是不是正确
        //验证码错误，返回错误提示
        //验证码正确，再去判断用户名和密码是否正确
        String codeInSession = (String) session.getAttribute("codeInSession");
        if (!codeInSession.equalsIgnoreCase(code)) {
            return Result.error("验证码错误");
        }

        Admin admin = adminService.login(name, password);
        //卫操作
        if (admin == null) {
            return Result.error("用户名或密码错误");
        }

        if (admin.getStatus() == 0) {
            return Result.error("该用户已经禁用，请联系管理员");
        }

        List<Permission> permissionList = permissionService.selectByRole(admin.getRole());
        session.setAttribute("permissionList", permissionList);

        //登录成功
        session.setAttribute("admin", admin);
        return Result.ok("登录成功", admin);
    }

    @RequestMapping("/update")
    public Result update(Admin admin) {
        adminService.updateById(admin);
        return Result.ok("修改成功");
    }

    @RequestMapping("/selectById")
    public Result<Admin> selectById(Integer id) {
        Admin admin = adminService.getById(id);
        return Result.ok(admin);
    }

    @RequestMapping("/add")
    public Result add(Admin admin) {
        adminService.save(admin);
        //将真正用到保存到数据库中的图片放到Redis的另一个集合里面
        redisTemplate.opsForSet().add(RedisConstants.UPLOAD_IMAGE_TO_DB, admin.getAvatar());
        return Result.ok("添加成功");
    }

    // /admin/list?page=1&limit=10
    // /admin/list?page=1&limit=10&name=zhangsan
    // /admin/list?page=1&limit=10&name=zhangsan&beginCreateDate
    @RequestMapping("/list")
    public PageResult list(AdminQuery adminQuery) {
        PageResult pageResult = adminService.list(adminQuery);
        return pageResult;
    }

    @RequestMapping("/deleteById")
    public Result deleteById(Integer id) {
        adminService.removeById(id);
        return Result.ok("删除成功");
    }

    @RequestMapping("/deleteAll")
    public Result deleteAll(Integer[] ids) {
        System.out.println(Arrays.toString(ids));
        adminService.removeBatchByIds(Arrays.asList(ids));
        return Result.ok("删除成功");
    }


    /*@RequestMapping("/toAdminList")
    public String toAdminList() {
        return "admin_list";
    }*/
}

